import Toast from './Toast.vue'
import { createApp, h } from 'vue'
let currentToast
export const openToast = (content, options) => {
  if (currentToast) {
    currentToast.app.unmount(currentToast.div)
    currentToast.div.remove()
  }
  currentToast = createToast(content, options)
}

function createToast(content, options) {
  const div = document.createElement('div')
  const { callback, enableHtml, autoClose, position } = options
  const app = createApp({
    render() {
      return h(
        Toast,
        {
          callback,
          htmlContent: content,
          enableHtml,
          autoClose,
          position,
        },
        { content }
      )
    },
  })
  app.mount(div)
  return { app, div }
}
